package com.visikard.ui.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.item.ItemTransactionRewardPunchKard;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class TransactionHistoryRewardPunchAdapter extends BaseAdapter {

	Context context;
	ArrayList<ItemTransactionRewardPunchKard> data;
	LayoutInflater inflater;

	DateFormat formatter;
	DateFormat dateOnlyFormatter;
	DateFormat timeFormat;
	Handler mHandler;
	DisplayMetrics metrics;

	public TransactionHistoryRewardPunchAdapter(Context ct,
			ArrayList<ItemTransactionRewardPunchKard> dt, Handler mHandler) {
		this.context = ct;
		this.data = dt;
		this.mHandler = mHandler;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR);
		dateOnlyFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_FULL_MONTH_DATE);
		timeFormat = new SimpleDateFormat(Constant.TIME_SHORT_FORMAT_STR, Locale.US);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	private class ViewHolder {
		TextView mUsername, mTextHeader, mTimeZone, mGiftKardSent;
		ImageView imgKard;
		RelativeLayout lyHeader;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_transaction_history_reward_punch_kard, parent, false);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();	
			
			viewHolder.lyHeader = (RelativeLayout) convertView.findViewById(R.id.item_transaction_header);			
			viewHolder.mTextHeader = (TextView) convertView.findViewById(R.id.transaction_text_header);
			viewHolder.mUsername = (TextView) convertView.findViewById(R.id.text_transaction_username);
			viewHolder.mTimeZone = (TextView) convertView.findViewById(R.id.text_transaction_timezone);			
			viewHolder.mGiftKardSent = (TextView) convertView.findViewById(R.id.txt_kard_sent);
			
			viewHolder.imgKard = (ImageView) convertView.findViewById(R.id.image_kard);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		ItemTransactionRewardPunchKard item = (ItemTransactionRewardPunchKard) getItem(position);
		if (item != null) {

			viewHolder.mUsername.setText(item.getUsername());
			
			if (!item.getKardImage().isEmpty())
//				ImageLoader.getInstance(context).loadImageCorner(
//						viewHolder.imgKard, item.getKardImage(),
//						Device.getDeviceWidth() / 4, BitmapUtils.ROUND_ALL, 4,
//						3, R.drawable.kard_shadow_default_horizontal, 0, 0);
				UILImageUtils.loadImage(viewHolder.imgKard, //image view
						item.getKardImage(), //url
						1, //size of view
						BitmapUtils.ROUND_ALL, //round all 
						4, // round size
						3, //shadow
						R.drawable.kard_shadow_default_horizontal, //shadow resource 
						0, 0);
			

			try {
//				Date today = dateOnlyFormatter.parse(dateOnlyFormatter
//						.format(new Date()));
//				Date date = dateOnlyFormatter
//						.parse(dateOnlyFormatter.format(formatter.parse(item
//								.getDateTimeWithTimezone())));
				Date today = new Date();
				Date date = new Date(item.getDateTime());

				if (position < 1) {
					String strDate = "";
					if (Utils.compareDateOnly(date, today) == 0) {
						strDate = context.getResources().getString(R.string.literal_today) +", "; 						
					}
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);
					strDate += dateOnlyFormatter.format(date);
					viewHolder.mTextHeader.setText(strDate);
					viewHolder.lyHeader.setVisibility(View.VISIBLE);
				} else {
//					Date date_above = dateOnlyFormatter
//					.parse(dateOnlyFormatter.format(formatter
//							.parse(data.get(position - 1)
//									.getDateTimeWithTimezone())));
					Date date_above = new Date(data.get(position - 1).getDateTime());
					if (Utils.compareDateOnly(date, date_above) != 0) {
						Calendar cal = Calendar.getInstance();
						cal.setTime(date);
						viewHolder.mTextHeader.setText(dateOnlyFormatter.format(date));
						viewHolder.lyHeader.setVisibility(View.VISIBLE);
					} else
						viewHolder.lyHeader.setVisibility(View.GONE);
				}
				viewHolder.mTimeZone.setText(timeFormat.format(formatter.parse(item.getDateTimeWithTimezone())));
				
				//gift cark content
				String strGiftKardContent = String.format(context.getString(R.string.txt_giftcard_has_been_sent), item.getOriginalAmount());
				viewHolder.mGiftKardSent.setText(strGiftKardContent);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return convertView;
	}
	
}
